﻿#include "stdafx.h"
#include "public.h"

void WriteFile(const char* filename, char* data, int len)
{
    FILE *fp = NULL;
    fp = fopen(filename, "a+");
    int length = fwrite(data, len, 1, fp);
    fclose(fp);
}


IplImage* cvCvtDIB2IMG(CDIB * pDib)
{
    IplImage * pImg;
    if (pDib->m_bitcount == 8)
        pImg = cvCreateImage(cvSize(pDib->Width(), pDib->Height()),
        IPL_DEPTH_8U, 1);
    else
        pImg = cvCreateImage(cvSize(pDib->Width(), pDib->Height()),
        IPL_DEPTH_8U, 3);

    memcpy(pImg->imageDataOrigin, pDib->m_pBits, pDib->GetBitSize());
    return pImg;
}

CDIB* cvCvtIMG2DIB(IplImage * pImg)
{
    CDIB * pDib = new CDIB;

    if (pImg->nChannels == 1)
        pDib->CreateDIB(pImg->width, pImg->height, 8);
    else
        pDib->CreateDIB(pImg->width, pImg->height, 24);

    memcpy(pDib->m_pBits, pImg->imageDataOrigin, pImg->imageSize);
    return pDib;
}

